package crivia.ecp.carry.em;

import crivia.ecp.carry.html.div.Div;
import crivia.ecp.carry.html.table.Table;
import crivia.ecp.common.ECR;
import crivia.ecp.exception.WrongDocumentObject;
import crivia.txt.sc.RK;

public class ScrollableTableGroup {

	public final static String ScrollType_Column = "C";
	public final static String ScrollType_Row = "R";
	
	private Table tableFixed;
	public Table getTableFixed() {
		return tableFixed;
	}
	public ScrollableTableGroup setTableFixed(Table tableFixed) {
		this.tableFixed = tableFixed;
		return this;
	}
	
	private Table tableScroll;
	public Table getTableScroll() {
		return tableScroll;
	}
	public ScrollableTableGroup setTableScroll(Table tableScroll) {
		this.tableScroll = tableScroll;
		return this;
	}
	
	private String width = "100%";
	public String getWidth() {
		return width;
	}
	public ScrollableTableGroup setWidth(String width) {
		this.width = width;
		return this;
	}
	
	private String scrollRange = "70%";
	public String getScrollRange() {
		return scrollRange;
	}
	public void setScrollRange(String scrollRange) {
		this.scrollRange = scrollRange;
	}
	
	private String scrollType = ScrollType_Column;
	public String getScrollType() {
		return scrollType;
	}
	public ScrollableTableGroup setScrollType(String scrollType) {
		this.scrollType = scrollType;
		return this;
	}
	
	private String key = "_"+RK.rk(10);
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	
	@Override
	public String toString() {
		if (ScrollType_Column.equals(scrollType)){
			String allRange = ECR.ns(width).replaceAll(" ", "");
			String scrollRange = ECR.ns(this.scrollRange).replaceAll(" ", "");
			String fixedRange;
			if (true
			&& allRange.endsWith("%")
			&& scrollRange.endsWith("%") ){
				fixedRange = (0
					+ ECR.toNumber(allRange.replaceAll("[%]", ""), int.class)
					- ECR.toNumber(scrollRange.replaceAll("[%]", ""), int.class)) + "%";
			} else if (true
			&& allRange.endsWith("px") 
			&& scrollRange.endsWith("px")){
				fixedRange = (0
					+ ECR.toNumber(allRange.replaceAll("px", ""), int.class)
					- ECR.toNumber(scrollRange.replaceAll("px", ""), int.class)) + "px";
			} else {
				throw new WrongDocumentObject("Wrong All Range & Scroll Range Format."
					+"\n > All Range : "+
						(allRange.endsWith("%") ? "%"
						: allRange.endsWith("px") ? "px"
						: allRange)
					+"\n > Scroll Range : "+
						(scrollRange.endsWith("%") ? "%"
						: scrollRange.endsWith("px") ? "px"
						: scrollRange));
			}
			return "" 
			+Div.createByID(key).setInner(""
				+Div.createByID(key+"_scroll").setInner(""
					+tableScroll)
					.addStyle("width", scrollRange)
					.addStyle("float", "right")
					.addStyle("overflow-x", "scroll")
					.addStyle("overflow-y", "hidden")
				+Div.createByID(key+"_fixed").setInner(""
					+tableFixed)
					.addStyle("width", fixedRange)
					.addStyle("float", "right")
					.addStyle("overflow", "hidden")
				).addStyle("width", width);
		} else {
			return "" 
			+Div.createByID(key).setInner(""
				+Div.createByID(key+"_fixed").setInner(""
					+tableFixed)
					.addStyle("overflow", "hidden")
				+Div.createByID(key+"_scroll").setInner(""
					+tableScroll)
					.addStyle("height", scrollRange)
					.addStyle("overflow-y", "scroll")
					.addStyle("overflow-x", "hidden"))
				.addStyle("width", width);
		}
	}

}
